<template>
  <div>
    <h1>品牌列表</h1>
    <div>
      <el-form :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item label="品牌名称">
          <el-input
            v-model="searchForm.brandName"
            placeholder="请输入"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="loadList">查询</el-button>
          <el-button type="primary" @click="handleAddBrand">添加</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="brandId" label="品牌ID" />
        <el-table-column prop="brandName" label="品牌名称" />
        <el-table-column prop="brandUse" label="使用数量" />
        <el-table-column prop="brandTop" label="排序" />
        <el-table-column prop="brandStatus" label="状态">
          <template #default="scope">
            <el-switch
              v-model="scope.row.brandStatus"
              inline-prompt
              active-text="显示"
              inactive-text="隐藏"
              :active-value="0"
              :inactive-value="1"
              @change="onChangeStatus(scope.row)"
            />
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" />
        <el-table-column label="操作" />
      </el-table>
    </div>

    <div class="demo-pagination-block">
      <el-pagination
        v-model:current-page="searchForm.pageNum"
        v-model:page-size="searchForm.pageSize"
        :page-sizes="[2, 4, 6, 8]"
        :size="size"
        :disabled="disabled"
        :background="background"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <div>
      <el-dialog v-model="dialogFormVisible" title="品牌添加" width="500">
        <el-form :model="form" ref="ruleFormRef" :rules="rules">
          <el-form-item
            label="品牌名称"
            :label-width="formLabelWidth"
            prop="brandName"
          >
            <el-input v-model="form.brandName" autocomplete="off" />
          </el-form-item>
          <el-form-item
            label="排序"
            :label-width="formLabelWidth"
            prop="brandTop"
          >
            <el-input-number v-model="form.brandTop" :min="1" />
          </el-form-item>
          <el-form-item
            label="状态"
            :label-width="formLabelWidth"
            prop="brandStatus"
          >
            <el-radio-group v-model="form.brandStatus">
              <el-radio :value="0">显示</el-radio>
              <el-radio :value="1">隐藏</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="dialogFormVisible = false">放弃</el-button>
            <el-button type="primary" @click="submitForm(ruleFormRef)">
              提交
            </el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { onMounted, reactive, ref } from "vue";

import {
  ElMessage,
  type ComponentSize,
  type FormInstance,
  type FormRules,
} from "element-plus";
import { getBrandList, changeBrandStatus, addBrand } from "../api/api";
const dialogFormVisible = ref(false);
const formLabelWidth = "140px";
const form = ref({
  brandId: 0,
  brandName: "",
  brandStatus: null,
  brandTop: null,
  brandUse: 0,
  createBy: "",
  createTime: "",
  updateBy: "",
  updateTime: "",
});

const tableData = ref([]);
const searchForm = ref({
  pageNum: 1,
  pageSize: 2,
  brandName: "",
});
const total = ref(0);

const size = ref<ComponentSize>("default");
const background = ref(false);
const disabled = ref(false);

const handleSizeChange = (val: number) => {
  console.log(`${val} items per page`);
  searchForm.value.pageSize = val;
  loadList();
};
const handleCurrentChange = (val: number) => {
  console.log(`current page: ${val}`);
  searchForm.value.pageNum = val;
  loadList();
};
const loadList = () => {
  getBrandList(searchForm.value).then((res) => {
    if (res.data.code == 200) {
      tableData.value = res.data.data.records;
      total.value = res.data.data.total;
    }
  });
};

const onChangeStatus = (row: any) => {
  changeBrandStatus(row).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success("修改成功");
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};
const ruleFormRef = ref<FormInstance>();
const rules = reactive<FormRules<any>>({
  brandName: [{ required: true, message: "请输入品牌名", trigger: "blur" }],
  brandTop: [{ required: true, message: "请输入排序", trigger: "blur" }],

  brandStatus: [
    {
      required: true,
      message: "请选择状态",
      trigger: "change",
    },
  ],
});

//品牌添加
const handleAddBrand = () => {
  dialogFormVisible.value = true;
};

//校验后提交
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      addBrand(form.value).then((res) => {
        if (res.data.code == 200) {
          ElMessage.success("添加成功");
          dialogFormVisible.value = false;
          loadList();
        } else {
          ElMessage.error(res.data.msg);
        }
      });
    } else {
      console.log("error submit!", fields);
    }
  });
};

onMounted(() => {
  loadList();
});
</script>

<style scoped>
.demo-pagination-block + .demo-pagination-block {
  margin-top: 10px;
}
.demo-pagination-block .demonstration {
  margin-bottom: 16px;
}
</style>
